package xin.exec.executor.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import xin.exec.executor.service.SimpleAsyncService;
import xin.exec.executor.service.TransactionalService;

import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * 异步调用测试控制器
 * {@link org.springframework.scheduling.annotation.EnableAsync} 注解:
 *
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2019/8/9
 */
@Controller
@RequestMapping
@EnableAsync
@Slf4j
public class ExecController {
    
    @Autowired
    private SimpleAsyncService service;
    
    @Autowired
    private TransactionalService transactionalService;

    @PostMapping
    public ResponseEntity<String> task() {

        try {
            service.task1();
            service.task2();
            service.task3();
            service.task4();
            service.task5();
        } catch (Exception e) {
            return ResponseEntity.ok(e.getMessage());
        }
        return ResponseEntity.ok("ok");
    }

    @PostMapping("testThrow")
    public ResponseEntity<String> testThrow() {

        try {
            Map<String ,Object> p3 = new HashMap<>();
            p3.put("name","张三");
            p3.put("sex","男");
            p3.put("age",18);
            service.testThrow("参数1","参数2",p3);
        } catch (Exception e) {
            return ResponseEntity.ok(e.getMessage());
        }
        return ResponseEntity.ok("ok");
    }
    
    @PostMapping("transNormal")
    public ResponseEntity<String> transNormal() {
        
        try {
            log.info(" 代理对象 ：{}",transactionalService.getClass());
            transactionalService.normalMethod();
        } catch (Exception e) {
            return ResponseEntity.ok(e.getMessage());
        }
        return ResponseEntity.ok("ok");
    }
    
    @PostMapping("transAsync")
    public ResponseEntity<String> transAsync() {
        
        try {
            log.info(" 代理对象 ：{}",transactionalService.getClass());
            transactionalService.asyncMethod1();
        } catch (Exception e) {
            return ResponseEntity.ok(e.getMessage());
        }
        return ResponseEntity.ok("ok");
    }
    
    @PostMapping("transAsync2")
    public ResponseEntity<String> transAsync2() {
        
        try {
            log.info(" 代理对象 ：{}",transactionalService.getClass());
            transactionalService.asyncMethod2();
        } catch (Exception e) {
            return ResponseEntity.ok(e.getMessage());
        }
        return ResponseEntity.ok("ok");
    }
    
    
    @PostMapping("transAsync3")
    public ResponseEntity<String> transAsync3() {
        
        try {
            log.info(" 代理对象 ：{}",transactionalService.getClass());
            transactionalService.asyncMethod3();
        } catch (Exception e) {
            return ResponseEntity.ok(e.getMessage());
        }
        return ResponseEntity.ok("ok");
    }
}
